from frame_work.login import PcLogin
from frame_work.logger import Logger
from frame_work.setting import *
from frame_work.yaml_file import YAML
from frame_work.util import check_tid, assert_result_dict, get_jid, get_sql_info, generate_random_nums
from frame_work import menu_list

# logger = Logger(logger ='audit_order_or_report').get_logger()
logger = Logger(__name__).get_logger()


class ConfirmReturnGoods(object):
    def __init__(self, tid):
        self.tid = tid
        self.session = self.get_session()
        self.yaml = YAML(os.path.join(base_Dir, 'data', 'data.yaml'))

    def get_session(self):
        """
        返回session
        :return:
        """
        statement = """SELECT uname from sk_try where tid = %s"""
        params = (self.tid,)
        value = get_sql_info(statement, params, meta={
            "info": "查询活动商家名称uname"
        })

        login = PcLogin(value[0].get("uname"))
        login.pc_login()
        session = login.get_session()
        return session

    def confirm_return_goods(self, uname):
        """
        商家确认发货
        :param uname: 试客名字
        :return:
        """
        yaml_data = self.yaml.get_data('modshow_confirm_return_goods')
        val = get_jid(self.tid, uname)
        yaml_data.get('data').update({
            'jid': str(val[0].get('jid')),
        })
        response = self.session.post(
            yaml_data.get('url'),
            data=yaml_data.get('data'),
            headers=yaml_data.get('header')).text
        # promote = response.encode('utf-8').decode('unicode_escape')
        assert_result_dict(response, uname + '确认寄回商品')


def main():
    while True:
        # 显示功能菜单
        menu_list.modshow_confirm_return_goods()
        # 选择
        choise = input("请输入您的选择: ")
        if choise in ('q', 'Q'):
            break
        try:
            tid = int(input(u'请输入活动tid: '))
            status = check_tid(tid)
            if not status:
                raise Exception("活动tid不存在try表，请核实后在重新输入")
        except Exception as e:
            logger.error("reszon => %s" % e)
        else:
            goods = ConfirmReturnGoods(tid)
            if choise == '1':
                buyer_uname = input('请输入待确认发货的试客名称:\n')
                goods.confirm_return_goods(buyer_uname)
            else:
                logger.info("没有对应的选项,请按选项输入...")
                break


if __name__ == '__main__':
    main()
